package com.small.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.Common;
import com.small.commom.AsyncDataLoader.Callback;
import com.smell.R;

public class LoginActivity extends Activity { 
	 
	private EditText edt_name, edt_pass;
	private Button tv_reg;
	private Button btn_on;
	private LinearLayout btn_qq;
	private Action action;
	private String strName = "";
	private String strPass = "";
	int result = -1;
	private CheckBox chk_autoLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_view);
		action = new Action(LoginActivity.this);
		onView();
	}

	public void onView() {
		chk_autoLogin =(CheckBox) this.findViewById(R.id.chk_autologin);
		chk_autoLogin .setChecked(Common.isAutoLogin);
		edt_name = (EditText) this.findViewById(R.id.login_txt_name);
		edt_pass = (EditText) this.findViewById(R.id.login_txt_pass);
		tv_reg = (Button) this.findViewById(R.id.login_btn_reg);
		 
		btn_on = (Button) this.findViewById(R.id.login_btn_on);
		btn_qq = (LinearLayout) this.findViewById(R.id.qq_login_id);
		tv_reg.setOnClickListener(onClick);
		btn_on.setOnClickListener(onClick);
		btn_qq.setOnClickListener(onClick);
	} 

	public void doLogin() {
		strName = edt_name.getText().toString().trim();
		strPass = edt_pass.getText().toString().trim();
		if (strName.equals("")) {
			edt_name.setError("请输入帐号");
			return;
		}
		if (strPass.equals("")) {
			edt_pass.setError("请输入密码");
			return;
		}

		action.showProgressDialog("登录中...");
		new AsyncDataLoader(asyncCallback).execute(); 
	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {
			result = action.doLogin(strName, strPass);
		}

		@Override
		public void onPrepare() {
		}
		@Override
		public void onFinish() { 
			action.stopProgressDialog();
			switch (result) {
			case 0:
				action.showShortToast("用户名或密码错误...");
				break;
			case 1: 
				action.saveUserInfo(strName, strPass,chk_autoLogin.isChecked());
				action.showShortToast("登录成功...");
				LoginActivity.this.startActivity(new Intent(LoginActivity.this, UserCentreActivity.class));
				LoginActivity.this.finish();
				break;
			case -1:
				action.showShortToast("登录失败...");
				break;
			case -2:
				action.showShortToast("网络异常、登录失败...");
				break;

			default:
				break;
			}
		}
	};  
	
	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.login_btn_on:
				doLogin();
				break;
			case R.id.login_btn_qq:

				break;
			case R.id.login_btn_reg:
				LoginActivity.this.startActivity(new Intent(LoginActivity.this,	RegisterActivity.class));
				finish();
				break;
			}
		}
	};

	
} 